package com.yifeng.repo.controller.fadada.configure;

import com.yifeng.repo.controller.fadada.signature.FadadaSignatureManager;
import com.yifeng.repo.controller.fadada.web.FadadaServletRegistrar;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * Created by daibing on 2024/11/23.
 */
@Configuration
@EnableConfigurationProperties(FadadaProperties.class)
@ConditionalOnClass({FadadaProperties.class})
public class FadadaConfiguration {

    @Value("${spring.application.name}")
    private String applicationName;

    @Resource
    private FadadaProperties properties;

    @Bean(initMethod = "init", destroyMethod = "destroy")
    @ConditionalOnProperty(prefix = FadadaProperties.PREFIX, name = "enable-fadada-signature")
    public FadadaSignatureManager fadadaSignatureManager() {
        return new FadadaSignatureManager(applicationName, properties);
    }

    @Bean
    @ConditionalOnProperty(prefix = FadadaProperties.PREFIX, name = "enable-admin-api")
    public FadadaServletRegistrar fadadaServletRegistrar(FadadaSignatureManager fadadaSignatureManager) {
        return new FadadaServletRegistrar(properties.getAdminApiPrefix(), fadadaSignatureManager);
    }
}
